import axios from 'axios'
import qs from 'qs'
axios.defaults.baseURL = 'http://172.18.84.23:8888/'
// axios.defaults.baseURL = 'http://172.18.84.31:8821/'
// const baseurl = 'http://172.18.84.23:8888/'
// axios.defaults.baseURL = baseurl
// axios.defaults.baseURL = 'http://172.18.84.31:8821/'


/**
 * baseurl
 */
// export default {
//     baseurl
// }

// let cicToken = window.sessionStorage.getItem("cicToken");
// let cicToken = "admin";
// axios.defaults.headers.common['cicToken'] = cicToken;

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {

    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    // console.log("http token start");
    // console.log(cicToken);
    // console.log("http token end");

    return new Promise((resolve, reject) => {
        axios.get(url, {
            headers: {
                cicToken: cicToken
            },
            params
        }).then(resp => {
            resolve(resp.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}

/** 
 * post方法，携带JSON 对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post_json(url, params) {
    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    return new Promise((resolve, reject) => {
        axios.post(url,
            // params: params,
            params, {
            headers: {
                cicToken: cicToken
            }
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

/** 
 * post方法，携带FORM 对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post_form(url, params) {
    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    return new Promise((resolve, reject) => {
        axios.post(url,
            // params: params,
            qs.stringify(params), {
            headers: {
                cicToken: cicToken
            }
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

/** 
 * deletes方法，对应delete请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function deletes(url, params) {
    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    return new Promise((resolve, reject) => {
        axios.delete(url, {
            headers: {
                cicToken: cicToken
            },
            params
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}


/** 
 * deletes方法，对应delete请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function deletes_json(url, params) {
    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    return new Promise((resolve, reject) => {
        axios.delete(url,
            params, {
            headers: {
                cicToken: cicToken
            }
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

/** 
 * put方法，携带json 对应put请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function put_json(url, params) {
    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    return new Promise((resolve, reject) => {
        axios.put(url,
            params, {
            headers: {
                cicToken: cicToken
            }
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}



/** 
 * put方法，携带json 对应put请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function put_form(url, params) {
    // 配置请求的跟路径
    let cicToken = window.localStorage.getItem("cicToken");

    return new Promise((resolve, reject) => {
        axios.put(url,
            qs.stringify(params), {
            headers: {
                cicToken: cicToken
            }
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}